import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.event.TableModelEvent;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;



public class PanelDerechoUsuario extends JPanel{

	
	private static final long serialVersionUID 	= 4L;
	private ServidorBaseDatos baseDatos			;
	private JButton busqueda					= new JButton("Buscar");
	//private Object data[][]						;
	String[] columnNames 						= {"nombre", "propietario","especie","sexo","nacimiento","fallecimiento"};
	DefaultTableModel modeloTabla 				= new DefaultTableModel(null, columnNames);
	JTable tabla	 							= new JTable(modeloTabla);
	JScrollPane scpEjemplo						= new JScrollPane(tabla);
	
	
	public PanelDerechoUsuario(ServidorBaseDatos baseDatos){
	
		this.baseDatos=baseDatos;
		this.setLayout(new GridBagLayout());
		agregarBorde();

		agregarBusqueda();
		agregarListado();
		agregarIngresoDatos();
	}
	private void agregarBorde(){
		
		Border raisedbevel, loweredbevel;
		raisedbevel = BorderFactory.createRaisedBevelBorder();
		loweredbevel = BorderFactory.createLoweredBevelBorder();
		Border compound;
		compound = BorderFactory.createCompoundBorder(raisedbevel, loweredbevel);
		this.setBorder(compound);
	}
	
	private void agregarBusqueda(){
		
		GridBagConstraints constraints = new GridBagConstraints();
				
		constraints.gridx = 0;
		constraints.gridy = 0;
		constraints.gridwidth = 3;
		constraints.gridheight = 1;
		constraints.fill = GridBagConstraints.BOTH;
		constraints.weighty = 0.0; 
		constraints.weightx = 1.0;
		constraints.ipady = 10;
		this.add (busqueda, constraints);
		
		busqueda.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				int lineas = modeloTabla.getRowCount();
				for(int i=0;i<lineas;i++){
					modeloTabla.removeRow(0);
				}
				
				try {
					
					ResultSet resultadoConsulta = baseDatos.query("select * from mascotas");
					for(int i=0;resultadoConsulta.next();i++){
						
						Object fila[] = {resultadoConsulta.getString(1),resultadoConsulta.getString(2),resultadoConsulta.getString(3),resultadoConsulta.getString(4),resultadoConsulta.getString(5),resultadoConsulta.getString(6)};
						modeloTabla.addRow(fila);
						System.out.println(resultadoConsulta.getString(1)+"\t"+resultadoConsulta.getString(2));
					}
					resultadoConsulta.close();
					
				} catch (NullPointerException e) {
					
				} catch (Exception e) {
				}
			}
		});
		
	}	
	private void agregarListado(){
		
		
		scpEjemplo.setPreferredSize(new Dimension(10,10));
		GridBagConstraints constraints = new GridBagConstraints();
		
		constraints.gridx = 0;
		constraints.gridy = 1;
		constraints.gridwidth = 3;
		constraints.gridheight = 1;
		constraints.fill = GridBagConstraints.BOTH;
		constraints.weighty = 1.0; 
		constraints.weightx = 1.0;
		constraints.ipady = 100;
		
		this.add (scpEjemplo, constraints); 
		
	}
	private void agregarIngresoDatos(){
		
		GridBagConstraints constraints = new GridBagConstraints();
		
		constraints.gridx = 0;
		constraints.gridy = 2;
		constraints.gridwidth = 3;
		constraints.gridheight = 1;
		constraints.fill = GridBagConstraints.BOTH;
		constraints.weighty = 0.0; 
		constraints.weightx = 1.0;
		
		constraints.ipady = 100;
		
		this.add (new JTextField("Ingreso Datos 2"), constraints); 
	}
}
